/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.runtime.impl.unit;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jess.JessException;
import jess.Rete;
import prototype.persistence.pojo.Position;
import prototype.persistence.pojo.UserHasUnit;

/**
 *
 * @author Server
 */
public class JESSHelper {

    private List<UserHasUnit> uhus;
    private UserHasUnit uhu;
    private Rete rete;
    private Position position;

    public JESSHelper(List<UserHasUnit> uhus, UserHasUnit uhu) {
        this.uhus = uhus;
        this.uhu = uhu;
        rete = new Rete();
        renew();
    }

    private void renew() {
        rete = new Rete();
        try {
            rete.batch(uhu.getUnit().getJessFile());
        } catch (JessException ex) {
            Logger.getLogger(JESSHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (UserHasUnit uhuLoc : uhus) {
            try {
                rete.add(uhuLoc);
            } catch (JessException ex) {
                Logger.getLogger(JESSHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            rete.add(this);
        } catch (JessException ex) {
            Logger.getLogger(JESSHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Position run() throws JessException {
        position = null;
        renew();
        rete.run();
        return position;
    }

    public void generateFirePosition(int x, int y, int z) {
        Position pos = new Position();

        pos.setX(x);
        pos.setY(y);
        pos.setZ(z);
        this.position = pos;
    }

    public Position getPosition() {
        return position;
    }

    public Rete getRete() {
        return rete;
    }

    public UserHasUnit getUhu() {
        return uhu;
    }

    public List<UserHasUnit> getUhus() {
        return uhus;
    }

    public void setPosition(Position position) {
        this.position = position;
    }

    public void setRete(Rete rete) {
        this.rete = rete;
    }

    public void setUhu(UserHasUnit uhu) {
        this.uhu = uhu;
    }

    public void setUhus(List<UserHasUnit> uhus) {
        this.uhus = uhus;
    }
}
